/*
 * SaveHublist.java
 *
 * Created on 16 de Dezembro de 2004, 15:39
 */

package ceirinhashls;

import java.util.*;
import java.io.*;
import java.text.*;
/**
 *
 * @author  botelhodaniel
 */
public class SaveHublist {
    private static String formaterPlain = "{0}|{1}|{2}|{3}|||||";
    private static String formaterXML = "\t\t<Hub Name=\"{0}\" Address=\"{1}\" Description=\"{2}\" Users=\"{3}\" Country=\"{4}\" Shared=\"{5}\" Status=\"{6}\" Minshare=\"{7}\" Minslots=\"{8}\" Maxhubs=\"{9}\" Maxusers=\"{10}\" Reliability=\"{11}\" Rating=\"{12}\" Port=\"{13}\" />";
    
    /** Creates a new instance of SaveHublist */
    public SaveHublist(Iterator it, String url) {
        try {
            BufferedWriter fMed=new BufferedWriter(new FileWriter(url));
            //Iterator it = hublist.iterator();
            if(url.toLowerCase().endsWith(".xml"))
                writeXMLFile(fMed,it);
            else{
                int i=0;
                while(it.hasNext()){
                    fMed.write(MessageFormat.format(formaterPlain, ((DCHub)it.next()).getData()));
                    fMed.newLine();
                }
            }
            fMed.flush();
            fMed.close();
        } catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("File created: "+url);
    }
    
     public SaveHublist(Iterator it, File file) {
        try {
            BufferedWriter fMed=new BufferedWriter(new FileWriter(file));
            //Iterator it = hublist.iterator();
            if(file.getName().toLowerCase().endsWith(".xml"))
                writeXMLFile(fMed,it);
            else{
                int i=0;
                while(it.hasNext()){
                    fMed.write(MessageFormat.format(formaterPlain, ((DCHub)it.next()).getData()));
                    fMed.newLine();
                }
            }
            fMed.flush();
            fMed.close();
        } catch (IOException e){
            e.printStackTrace();
        }
        System.out.println("File created: "+file);
    }
    private void writeXMLFile(BufferedWriter fMed,Iterator it) throws IOException{
        //fMed.write("<?xml version=\"1.0\" encoding=\"windows-1252\" ?>");
    	fMed.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
    	fMed.newLine();
        fMed.write("<Hublist Name=\"xml hublist\" Address=\"\">");
        fMed.newLine();
        fMed.write("\t<Hubs>");
        fMed.write("\n\t\t<Columns>"+
			"\n\t\t\t<Column Name=\"Name\" Type=\"string\"/>"+
			"\n\t\t\t<Column Name=\"Address\" Type=\"string\"/>"+
			"\n\t\t\t<Column Name=\"Description\" Type=\"string\"/>"+
			"\n\t\t\t<Column Name=\"Users\" Type=\"int\"/>"+
			"\n\t\t\t<Column Name=\"Country\" Type=\"string\"/>"+
			"\n\t\t\t<Column Name=\"Shared\" Type=\"bytes\"/>"+
			"\n\t\t\t<Column Name=\"Status\" Type=\"string\"/>"+
			"\n\t\t\t<Column Name=\"Minshare\" Type=\"bytes\"/>"+
			"\n\t\t\t<Column Name=\"Minslots\" Type=\"int\"/>"+
			"\n\t\t\t<Column Name=\"Maxhubs\" Type=\"int\"/>"+
			"\n\t\t\t<Column Name=\"Maxusers\" Type=\"int\"/>"+
			"\n\t\t\t<Column Name=\"Reliability\" Type=\"percent\"/>"+
			"\n\t\t\t<Column Name=\"Rating\" Type=\"string\"/>"+
			"\n\t\t\t<Column Name=\"Port\" Type=\"int\"/>"+
		"\n\t\t</Columns>");
        fMed.newLine();
        Object[] args;
        while(it.hasNext()){
            DCHub hub = ((DCHub)it.next());
            args = new Object[]{
                hub.getName(),//Name
                hub.getAddress(), //Address
                hub.getDescription(), //Description
                ""+hub.getUsers(), //Users
                hub.getCountry(), //Country
                hub.getShared(), //Shared
                hub.getStatus(), //Status
                hub.getMinshare(), //Minshare
                hub.getMinslots(), //Minslots
                hub.getMaxhubs(), //Maxhubs
                hub.getMaxusers(), //Maxusers
                hub.getReliability(), //Reliability
                hub.getRating(), //Rating
                ""+hub.getPort(), //Port
                
            };
            fMed.write(MessageFormat.format(formaterXML, args));
            fMed.newLine();
        }
        fMed.write("\n\t</Hubs>"+
                    "\n</Hublist>");
    }
}
